package com.survivor.example;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import com.survivor.activity.CoordHelper;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
import android.view.MotionEvent;

public class Lessone13Rendder implements Renderer {

	private CoordHelper ch;

	private float[] verCoord = new float[] { 
			-0.2f, -0.1f, 0f,
			0.2f, -0.1f, 0f,
			-0.2f, 0.1f, 0f,
			0.2f, 0.1f, 0f 
		};

	private float[] texCoord = { 0, 1, 1, 1, 0, 0, 1, 0 };

	private FloatBuffer verBuffer, texBuffer;

	private Bitmap bmp;

	private Paint mPaint;

	private int[] textures = new int[1];
	
	private RectF btnRect;
	
	private boolean isOnClick = false;

	public Lessone13Rendder(Context context) {
		// TODO Auto-generated constructor stub
		init(context);
	}

	private FloatBuffer makeFloatBuffer(float[] src) {
		ByteBuffer bb = ByteBuffer.allocateDirect(src.length * 4);
		bb.order(ByteOrder.nativeOrder());
		FloatBuffer fb = bb.asFloatBuffer();
		fb.put(src);
		fb.position(0);
		return fb;
	}

	private void init(Context context) {
		mPaint = new Paint();
		ch = new CoordHelper(context);

		System.out.println("ScreenWidth --> " + ch.w);
		System.out.println("ScreenHeight --> " + ch.h);

		bmp = Bitmap.createBitmap(ch.w / 5, ch.h / 10, Config.ARGB_8888);
		setBmpContent();
		
		btnRect = new RectF(ch.oglX2ScrX(-.2f), ch.oglY2ScrY(.1f), ch.oglX2ScrX(.2f), ch.oglY2ScrY(-.1f));

		verBuffer = makeFloatBuffer(verCoord);
		texBuffer = makeFloatBuffer(texCoord);

	}

	private void setBmpContent() {
		Canvas canvas = new Canvas(bmp);
		mPaint.setColor(Color.WHITE);
		mPaint.setAntiAlias(true);
		mPaint.setTextSize(30f);
		canvas.drawText("NeHe Example", 0, 35, mPaint);

	}

	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		doDraw(gl);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub

		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluPerspective(gl, 45f, (float) width / height, .1f, 10f);
		
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		loadTextures(gl);
	}

	private void doDraw(GL10 gl) {
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verBuffer);
		
		if(isOnClick)
			gl.glScalef(1.5f, 1.5f, 0);
		
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}

	private void loadTextures(GL10 gl) {
		gl.glClearColor(0f, 0f, 0f, 0f);

		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glGenTextures(textures.length, textures, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);

		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
				GL10.GL_LINEAR);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
				GL10.GL_LINEAR);
		

		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glClearDepthf(1f);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glShadeModel(GL10.GL_SMOOTH);
	}
	
	public void onTouchEvent(MotionEvent event)
	{
		float x = event.getX();
		float y = event.getY();
		int action = event.getAction();
		
		switch(action)
		{
		case MotionEvent.ACTION_DOWN:
			if(btnRect.contains(x, y))
				isOnClick = true;
			break;
		case MotionEvent.ACTION_UP:
			isOnClick = false;
			break;
		}
	}

}
